package com.hanxing.kxcharge.module.operations.controller.admin.costtemplate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.CommonResult;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;
import static com.hanxing.kxcharge.framework.common.pojo.CommonResult.success;

import com.hanxing.kxcharge.framework.excel.core.util.ExcelUtils;

import com.hanxing.kxcharge.framework.operatelog.core.annotations.OperateLog;
import static com.hanxing.kxcharge.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hanxing.kxcharge.module.operations.controller.admin.costtemplate.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.costtemplate.CostTemplateDO;
import com.hanxing.kxcharge.module.operations.service.costtemplate.CostTemplateService;

@Tag(name = "管理后台 - 计费模板")
@RestController
@RequestMapping("/operations/cost-template")
@Validated
public class CostTemplateController {

    @Resource
    private CostTemplateService costTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建计费模板")
    @PreAuthorize("@ss.hasPermission('operations:cost-template:create')")
    public CommonResult<Long> createCostTemplate(@Valid @RequestBody CostTemplateSaveReqVO createReqVO) {
        return success(costTemplateService.createCostTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计费模板")
    @PreAuthorize("@ss.hasPermission('operations:cost-template:update')")
    public CommonResult<Boolean> updateCostTemplate(@Valid @RequestBody CostTemplateSaveReqVO updateReqVO) {
        costTemplateService.updateCostTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计费模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('operations:cost-template:delete')")
    public CommonResult<Boolean> deleteCostTemplate(@RequestParam("id") Long id) {
        costTemplateService.deleteCostTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计费模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('operations:cost-template:query')")
    public CommonResult<CostTemplateRespVO> getCostTemplate(@RequestParam("id") Long id) {
        CostTemplateDO costTemplate = costTemplateService.getCostTemplate(id);
        return success(BeanUtils.toBean(costTemplate, CostTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计费模板分页")
    @PreAuthorize("@ss.hasPermission('operations:cost-template:query')")
    public CommonResult<PageResult<CostTemplateRespVO>> getCostTemplatePage(@Valid CostTemplatePageReqVO pageReqVO) {
        PageResult<CostTemplateDO> pageResult = costTemplateService.getCostTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CostTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计费模板 Excel")
    @PreAuthorize("@ss.hasPermission('operations:cost-template:export')")
    @OperateLog(type = EXPORT)
    public void exportCostTemplateExcel(@Valid CostTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CostTemplateDO> list = costTemplateService.getCostTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计费模板.xls", "数据", CostTemplateRespVO.class,
                        BeanUtils.toBean(list, CostTemplateRespVO.class));
    }

}